<?php

class ImageManager extends CI_Model {
	function __construct() {
		parent::__construct ();
		$this->load->database ();
		$this->load->model ( array ('vo/ImageVo', 'helper/ImageHelper', 'vo/Display_Item' ) );
	}
	
	public function save($imageVo) {
		$data = array ();
		
		$user = $this->session->userdata ( USER_KEY );
		
		if ($user) {
			$data ['folder_id'] = $imageVo->getFolderId ();
			$data ['cpy_id'] = $user ['cpyId'];
			$data ['image_name'] = $imageVo->getImageName ();
			
			date_default_timezone_set ( 'PRC' );
			$data ['image_uptime'] = date ( "y-m-d H:i:s" );
			
			$data ['image_vicnt'] = 0;
			$data ['image_desc'] = $imageVo->getImageDesc ();
			$data ['image_size'] = $imageVo->getImageSize ();
			$data ['image_url'] = $imageVo->getImageUrl ();
			$data ['product_size'] = $imageVo->getProductSize ();
			$data ['image_tru_url'] = $imageVo->getImageTruUrl ();
			$data ['image_creator'] = $user ['userId'];
			
			$this->db->insert ( 'fun_image', $data );
		}
	}
	
	public function update($imageVo) {
		
		$data = array ();
		if ($imageVo->getImageName ())
			$data ['image_name'] = $imageVo->getImageName ();
		
		if ($imageVo->getImageDesc ())
			$data ['image_desc'] = $imageVo->getImageDesc ();
		
		if ($imageVo->getImageSize ())
			$data ['image_size'] = $imageVo->getImageSize ();
		
		if ($imageVo->getImageUrl ())
			$data ['image_url'] = $imageVo->getImageUrl ();
		
		if ($imageVo->getImageTruUrl ())
			$data ['image_tru_url'] = $imageVo->getImageTruUrl ();
		
		if ($imageVo->getFolderId ())
			$data ['folder_id'] = $imageVo->getFolderId ();
			
		if ($imageVo->getProductSize())
			$data ['product_size'] = $imageVo->getProductSize ();
		
		$this->db->where ( 'fun_image_id', $imageVo->getFunImageId () );
		$user = $this->session->userdata ( USER_KEY );
		
		if ($user) {
			$this->db->where ( 'cpy_id', $user ['cpyId'] );
			$this->db->update ( 'fun_image', $data );
		}
	}
	
	public function countVistCnt($imageId) {
		$imageObj = $this->getImageById ( $imageId );
		
		if (! is_null ( $imageObj )) {
			$this->db->where ( 'fun_image_id', $imageId );
			$data ['image_vicnt'] = $imageObj->getImageVicnt () + 1;
			$this->db->update ( 'fun_image', $data );
		}
	}
	
	public function getImageById($imageId) {
		$this->db->where ( 'fun_image_id', $imageId );
		$query = $this->db->get ( 'fun_image' );
		
		if ($query->num_rows () <= 0)
			return NULL;
		
		$data = $query->row ();
		
		$imgObj = new ImageVo ( );
		$imgObj->setCpyId ( $data->cpy_id );
		$imgObj->setFunImageId ( $imageId );
		$imgObj->setImageVicnt ( $data->image_vicnt );
		$imgObj->setImageCreator ( $data->image_creator );
		$imgObj->setImageDesc ( $data->image_desc );
		$imgObj->setImageName ( $data->image_name );
		$imgObj->setImageSize ( $data->image_size );
		$imgObj->setImageTruUrl ( $data->image_tru_url );
		$imgObj->setImageUptime ( $data->image_uptime );
		$imgObj->setImageUrl ( $data->image_url );
		$imgObj->setFolderId ( $data->folder_id );
		$imgObj->setProductSize ( $data->product_size );
		
		$query->free_result ();
		return $imgObj;
	}
	
	public function delete($imageId) {
		if ($imageId)
			$result = $this->getImageById ( $imageId );
		if (isset ( $result ) && ! is_null ( $result )) {
			if ($result->getImageUrl ()) {
				$this->ImageHelper->del_file ( IMG_PATH . $result->getImageTruUrl () );
				$this->ImageHelper->del_file ( IMG_PATH . $result->getImageUrl () );
			}
			$user = $this->session->userdata ( USER_KEY );
			if ($user)
				$this->db->delete ( 'fun_image', array ('fun_image_id' => $result->getFunImageId (), 'cpy_id' => $user ['cpyId'] ) );
		}
	}
	
	public function getImagesByFolderId($folderId, $page = FALSE) {
		if ($folderId) {
			if ($page) {
				$page->setRows_num ( $this->countChildImages ( $folderId ) );
				$this->db->where ( 'folder_id', $folderId );
				$query = $this->db->get ( 'fun_image', $page->getPage_size (), $page->getStart_row () );
			} else {
				$this->db->where ( 'folder_id', $folderId );
				$query = $this->db->get ( 'fun_image' );
			}
			
			$data = array ();
			foreach ( $query->result () as $result ) {
				$data [$result->fun_image_id] = $result;
			}
			$query->free_result ();
			
			if ($page) {
				$page->setData ( $data );
				return $page;
			} else
				return $data;
		}
		return NULL;
	}
	
	function countChildImages($folderId) {
		$this->db->where ( 'folder_id', $folderId );
		$this->db->from ( 'fun_image' );
		return intval ( $this->db->count_all_results () );
	}
	
	public function getChildImages($folderId, $page = FALSE) {
		$childImages = $this->getImagesByFolderId ( $folderId, $page );
		if (is_null ( $childImages ))
			return FALSE;
		
		if (is_array ( $childImages )) {
			$childs = $childImages;
		} else {
			$childs = $childImages->getData ();
		}
		
		$images = array ();
		foreach ( $childs as $key => $result ) {
			$this->Display_Item = new Display_Item ( );
			$this->Display_Item->setId ( $key );
			$this->Display_Item->setName ( $result->image_name );
			$this->Display_Item->setDesc ( $result->image_desc );
			$this->Display_Item->setIsFolder ( FALSE );
			$this->Display_Item->setThumbImage ( base_url () . "resources/images/funImg/" . $result->image_tru_url );
			$this->Display_Item->setProductSize( $result->product_size );
			$this->Display_Item->setUrl ( base_url () . "image/index/" . $key );
			
			$images [$key] = $this->Display_Item;
		}
		
		if ($page) {
		   $page->setData($images);
		   return $page;
		} else
			return $images;
	}
}
?>